package com.zhida.smartcontrol.pagectrl.projectdetail.search.searchdetails;

import com.alibaba.fastjson.JSON;
import com.yanzhenjie.kalle.Params;
import com.zhida.smartcontrol.common.Api;
import com.zhida.smartcontrol.common.Const;
import com.zhida.smartcontrol.common.bean.LampBean;
import com.zhida.smartcontrol.common.bean.LampSwitchBean;
import com.zhida.smartcontrol.knet.KalleCallBack;
import com.zhida.smartcontrol.mvp.BasePresenterImpl;
import com.zhida.smartcontrol.pagectrl.projectdetail.search.ProjectSearchContract;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SearchDetailsPresenter extends BasePresenterImpl<SearchDetailsContract.View> implements SearchDetailsContract.Presenter {
    @Override
    public void requestLampData(String id) {
        Params params = Params.newBuilder()
                .add("id", id)
                .build();
        mKalleCenter.get(Api.GET_LAMP_SWITCH_BY_ID, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                try {
                    JSONObject jsonObject=new JSONObject(result);
                    switch (Integer.parseInt(jsonObject.getString("switchType"))){
                        case Const.NB_SINGLE:
                        case Const.PLC_SINGLE:
                        case Const.ZIG_BEE_SINGLE:
                        case Const.NB_DOUBLE:
                            LampSwitchBean lampSwitchBean = JSON.parseObject(result, LampSwitchBean.class);
                            mView.showData(lampSwitchBean);
                            break;
                        default:
                            LampBean lampBean = JSON.parseObject(result, LampBean.class);
                            mView.showData(lampBean);
                            break;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
    @Override
    public void runningModel(List<LampBean> codeList, int type) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);
        mKalleCenter.postJson(Api.RUNNING_MODEL, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
    @Override
    public void switchRepair(List<LampBean> codeList, int type) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        params.put("type", type);
        mKalleCenter.postJson(Api.SWITCH_REPAIR, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }

    @Override
    public void resetSwitch(List<LampBean> codeList) {
        List<String> list = new ArrayList<>();
        for (LampBean lampBean : codeList) {
            if (lampBean.isCheck()) {
                list.add(lampBean.getCode());
            }
        }
        if (list.size() <= 0) {
            mView.showMsg("请选择要操作的设备");
            return;
        }
        Map<String, Object> params = new HashMap<>();
        params.put("codeList", list);
        mKalleCenter.postJson(Api.RESET_SWITCH, params, true, new KalleCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
                mView.showMsg(msg);
            }

            @Override
            public void onFailed(String msg) {
                mView.showMsg(msg);
            }
        });
    }
}
